﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RobotJog.CApRJRobotJogCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using COMMANDPARAMSLib;
using EngineeringInternalExtension;
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.CommandParameters;
using Tecnomatix.Engineering.Olp;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.RobotJog;

public class CApRJRobotJogCmd : TxButtonCommand, ITxSemiModalCommandBehavior
{
  private readonly CApRJRobotJogEnabler _enabler;
  private CUiRJRobotJogWindow _robotJogWindow;
  private ITxObjectCollection _compoundEquipment;
  private static WeakReference _theCommand;

  public CApRJRobotJogCmd()
  {
    this._enabler = new CApRJRobotJogEnabler();
    CApRJRobotJogCmd._theCommand = new WeakReference((object) this);
  }

  public virtual bool Licensed => TxCommandLicenser.RobotJogLicense;

  public virtual ITxCommandEnabler CommandEnabler => (ITxCommandEnabler) this._enabler;

  public virtual string Bitmap => "RobotJog.Resources.RobotJog.bmp";

  public virtual string LargeBitmap => "RobotJog.Resources.RobotJog_LARGE.png";

  public virtual string Category => RJRobotJogResources.RobotJogCategory;

  public virtual string Description => RJRobotJogResources.RobotJogDescription;

  public virtual string Name => RJRobotJogResources.RobotJogName;

  public virtual string Tooltip => RJRobotJogResources.RobotJogTooltip;

  public CUiRJRobotJogWindow TheWindow => this._robotJogWindow;

  public static CApRJRobotJogCmd TheCommand
  {
    get
    {
      return CApRJRobotJogCmd._theCommand != null ? CApRJRobotJogCmd._theCommand.Target as CApRJRobotJogCmd : (CApRJRobotJogCmd) null;
    }
  }

  public virtual ITxImage Image => (ITxImage) TxImageManager.GetImageByKey("RobotJog");

  public virtual void Execute(object cmdParams)
  {
    if (!this._enabler.Enable)
      return;
    TxPerformanceMeasurementEx.StartMeasureTask("Start RobotJog");
    ITxObject itxObject = (ITxObject) null;
    ITxObjectCollection compoundEquipment = (ITxObjectCollection) null;
    TxObjectList items = TxApplication.ActiveSelection.GetItems();
    if (((Collection<ITxObject>) items).Count == 1)
    {
      if (((Collection<ITxObject>) items)[0] is ITxComponent && (((Collection<ITxObject>) items)[0] as ITxComponent).IsEquipment && ((Collection<ITxObject>) items)[0] is ITxObjectCollection)
      {
        TxObjectList allDescendants = (((Collection<ITxObject>) items)[0] as ITxObjectCollection).GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxRobot)));
        if (allDescendants != null && ((Collection<ITxObject>) allDescendants).Count > 0)
        {
          this._compoundEquipment = ((Collection<ITxObject>) items)[0] as ITxObjectCollection;
          if (((Collection<ITxObject>) allDescendants).Count > 1)
            compoundEquipment = ((Collection<ITxObject>) items)[0] as ITxObjectCollection;
          itxObject = ((Collection<ITxObject>) allDescendants)[0];
        }
      }
      if (itxObject == null)
      {
        if (((Collection<ITxObject>) items)[0] is ITxRoboticLocationOperation loc)
        {
          if (!this.CanManipulateLocation(loc) || this._robotJogWindow != null && CUiRJRobotJogWindow.IsLocationAddedBefore.HasValue)
            return;
          itxObject = (ITxObject) loc;
        }
        else if (((Collection<ITxObject>) items)[0] is TxRobot)
          itxObject = ((Collection<ITxObject>) items)[0];
      }
    }
    ITxRoboticLocationOperation referenceLocation = this.GetReferenceLocation(cmdParams);
    if (referenceLocation != null)
      CUiRJRobotJogWindow.IsLocationAddedBefore = new bool?(this.IsLocationAddedBefore(referenceLocation, itxObject as ITxRoboticLocationOperation));
    this.OpenRobotJog(itxObject, compoundEquipment);
    TxPerformanceMeasurementEx.StopMeasureTask("Start RobotJog");
  }

  bool ITxSemiModalCommandBehavior.StayEnabledInSemiModalMode
  {
    get
    {
      return TxApplicationEx.IsInSpecificMode(new TxApplicationModeEx("KINEMATICS_MOVEMENTS_MODE", true));
    }
  }

  private void OpenRobotJog(ITxObject manipulatedObject, ITxObjectCollection compoundEquipment)
  {
    TxApplicationEx.FreezeDisplay();
    bool renderFreezeState = TxGraphicViewerEx.GlobalRenderFreezeState;
    if (!renderFreezeState)
      TxGraphicViewerEx.GlobalRenderFreezeState = true;
    try
    {
      if (this._robotJogWindow != null)
        ((Window) this._robotJogWindow).Close();
      this._robotJogWindow = compoundEquipment == null ? new CUiRJRobotJogWindow(manipulatedObject) : new CUiRJRobotJogWindow(manipulatedObject, compoundEquipment);
      ((Window) this._robotJogWindow).Closed += new EventHandler(this._robotJogWindow_Closed);
      if (!this._robotJogWindow.WasForcedToClose)
        ((Window) this._robotJogWindow).Show();
      else
        ((Window) this._robotJogWindow).Close();
    }
    finally
    {
      TxApplicationEx.UnfreezeDisplay();
      if (!renderFreezeState)
        TxGraphicViewerEx.GlobalRenderFreezeState = false;
      TxApplication.RefreshDisplay();
    }
  }

  private void _robotJogWindow_Closed(object sender, EventArgs e)
  {
    this._robotJogWindow = (CUiRJRobotJogWindow) null;
    if (this._compoundEquipment == null)
      return;
    TxObjectList txObjectList = new TxObjectList();
    ((Collection<ITxObject>) txObjectList).Add((ITxObject) this._compoundEquipment);
    TxApplication.ActiveSelection.SetItems(txObjectList);
    this._compoundEquipment = (ITxObjectCollection) null;
  }

  private bool IsLocationAddedBefore(
    ITxRoboticLocationOperation referenceLocation,
    ITxRoboticLocationOperation newLocation)
  {
    ITxOrderedCompoundOperation roboticOperation = newLocation.ParentRoboticOperation as ITxOrderedCompoundOperation;
    int indexOfChild1 = ((ITxOrderedObjectCollection) roboticOperation).GetIndexOfChild((ITxObject) newLocation);
    ITxOperation itxOperation = referenceLocation is TxRoboticSeamLocationOperation ? ((ITxObject) referenceLocation).Collection as ITxOperation : (ITxOperation) referenceLocation;
    int indexOfChild2 = ((ITxOrderedObjectCollection) roboticOperation).GetIndexOfChild((ITxObject) itxOperation);
    return indexOfChild1 < indexOfChild2;
  }

  private ITxRoboticLocationOperation GetReferenceLocation(object cmdParams)
  {
    TxSingleObjectParameters objectParameters = CApRJRobotJogCmd.GetSingleObjectParameters(cmdParams);
    return objectParameters != null ? objectParameters.Object as ITxRoboticLocationOperation : (ITxRoboticLocationOperation) null;
  }

  private static TxSingleObjectParameters GetSingleObjectParameters(object cmdParams)
  {
    switch (cmdParams)
    {
      case TxSingleObjectParameters objectParameters2:
        return objectParameters2;
      case ICoSingleObjectParams singleObjectParams:
        TxSingleObjectParameters objectParameters1 = new TxSingleObjectParameters();
        ITApiObj apiObj = (ITApiObj) null;
        singleObjectParams.GetObj(ref apiObj);
        if (apiObj != null)
        {
          ITxObject objectFromApiObj = CApRJRobotJogCmd.GetObjectFromApiObj(apiObj);
          objectParameters1.Object = objectFromApiObj;
        }
        return objectParameters1;
      default:
        return (TxSingleObjectParameters) null;
    }
  }

  private static ITxObject GetObjectFromApiObj(ITApiObj apiObj)
  {
    string str;
    apiObj.DumpToString(ref str);
    return str != null ? TxConvertor.tID2Object(str) : (ITxObject) null;
  }

  private bool CanManipulateLocation(ITxRoboticLocationOperation loc)
  {
    ITxRoboticOrderedCompoundOperation roboticOperation = loc.ParentRoboticOperation;
    if (roboticOperation == null || !((ITxRoboticOperation) loc).IsMountedWorkpieceOperation)
      return true;
    ITxRobot robot = roboticOperation.Robot;
    if (robot == null)
      return true;
    ITxRoboticControllerServices controllerServices = CApRJRobotJogCmd.GetRoboticControllerServices(robot);
    if (controllerServices == null)
      return true;
    ITxGun activeGun = controllerServices.GetActiveGun((ITxLocationOperation) loc);
    return activeGun == null || !TxFrame.op_Equality((activeGun as ITxTool).TCPF, (ITxObject) null) || this.CanProceedForExternalGunWithoutTCPF(activeGun, loc);
  }

  private static ITxRoboticControllerServices GetRoboticControllerServices(ITxRobot robot)
  {
    TxRobot txRobot = robot as TxRobot;
    if (TxRobot.op_Equality(txRobot, (ITxObject) null))
      return (ITxRoboticControllerServices) null;
    TxRobotController controller = robot.Controller;
    if (controller == null)
      return (ITxRoboticControllerServices) null;
    TxOlpControllerUtilities controllerUtilities = new TxOlpControllerUtilities();
    ITxRoboticControllerServices controllerServices = (ITxRoboticControllerServices) controllerUtilities.GetInterfaceImplementationFromController(controller.Name, typeof (ITxRoboticControllerServices), typeof (TxControllerAttribute), "ControllerName") ?? (ITxRoboticControllerServices) controllerUtilities.GetInterfaceImplementationFromController("default", typeof (ITxRoboticControllerServices), typeof (TxControllerAttribute), "ControllerName");
    controllerServices?.Init(txRobot);
    return controllerServices;
  }

  private bool CanProceedForExternalGunWithoutTCPF(
    ITxGun externalGun,
    ITxRoboticLocationOperation loc)
  {
    return CApRJRobotJogNotificationService.Notify(string.Format(RJRobotJogResources.MSG_EXTERNAL_GUN_WITHOUT_TCP, (object) ((ITxObject) externalGun).Name, (object) ((ITxObject) loc).Name), base.Name, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;
  }
}
